package main

import "fmt"

const LENGTH = 10

var maze = [LENGTH][LENGTH]int{}

func InitMaze() {
	for i := 0; i < LENGTH; i++ {
		for j := 0; j < LENGTH; j++ {
			maze[i][j] = 0
			if i == 0 || j == 0 || i == LENGTH-1 || j == LENGTH-1 {
				maze[i][j] = 1
			}
		}
	}
}
func PrintMaze() {
	for i := 0; i < LENGTH; i++ {
		for j := 0; j < LENGTH; j++ {
			fmt.Printf("%d ", maze[i][j])
		}
		fmt.Printf("\n")
	}
} // 2 表示通， 3 表示不通顺
func WalkMaze(maze *[LENGTH][LENGTH]int, i int, j int) bool {
	if maze[LENGTH-2][LENGTH-2] == 2 {
		return true
	} else {
		if maze[i][j] == 0 {
			maze[i][j] = 2
			if WalkMaze(maze, i+1, j) {
				return true
			} else if WalkMaze(maze, i, j+1) {
				return true
			} else if WalkMaze(maze, i-1, j) {
				return true
			} else if WalkMaze(maze, i, j-1) {
				return true
			} else {
				maze[i][j] = 3
				return false
			}
		} else { //  1 2 3 情况
			return false
		}
	}
}

// 递归走迷宫
func main() {
	InitMaze()
	maze[2][1] = 1 // 设置屏障
	maze[2][2] = 1 // 设置屏障
	maze[2][3] = 1 // 设置屏障
	PrintMaze()
	WalkMaze(&maze, 1, 1)
	fmt.Println("-------------------------------")
	PrintMaze()
}
